package org.bonorsoft.modules.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.modules.wechat.entity.WxPicture;
import org.bonorsoft.modules.wechat.service.IPicturePhotoService;
import org.bonorsoft.modules.wechat.service.IWxPictureService;
import org.bonorsoft.modules.wechat.vo.PictureVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小程序端轮播图获取
 */
@Slf4j
@Controller
@RequestMapping("/wechat/picture")
public class WxPictureController extends BonorController<WxPicture, IWxPictureService> {
    @Autowired
    private IWxPictureService wxPictureService;
    @Autowired
    private IPicturePhotoService picturePhotoService;

    @GetMapping("/list")
    @ResponseBody
    public Result<?> pictureList() {
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        List<String> ids = wxPictureService.getIds();
        List<PictureVo> photos = new ArrayList<PictureVo>();
        for (String id : ids) {
            PictureVo photo = new PictureVo();
            photo.setPhotoLink(wxPictureService.getLink(id));
            photo.setPhotoPath(picturePhotoService.getPhoto(id));
            photos.add(photo);
        }
        obj.put("photos", photos);
        result.setResult(obj);
        result.success("查询成功！");
        return result;
    }
}
